package com.github.springboot.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.http.MediaType;
import springfox.documentation.spring.web.json.Json;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by kjtang on 2018/1/18.
 */
public class CustomFastJsonHttpMessageConverter extends FastJsonHttpMessageConverter {

    public CustomFastJsonHttpMessageConverter() {
        // 在这里配置 fastjson 特性
        //添加fastjson的配置信息，比如：是否需要格式化返回的json数据
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");    // 自定义时间格式
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); // 正常转换 null 值
        //fastjson版本1.2.15+不需要该配置
        fastJsonConfig.getSerializeConfig().put(Json.class,CustomSwaggerJsonSerializer.instance);
        //处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        this.setSupportedMediaTypes(fastMediaTypes);
        //将converter添加到converters中
        this.setFastJsonConfig(fastJsonConfig);
    }

    @Override
    protected boolean supports(Class<?> clazz) {
        return super.supports(clazz);
    }
}
